package org.exercise.record.controller;

import jakarta.servlet.http.HttpServletRequest;
import org.exercise.record.domain.User;
import org.exercise.record.service.UserService;
import org.exercise.record.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.ArrayList;
import java.util.List;

import static org.exercise.record.utils.ResponseUtils.makeErrorResponse;
import static org.exercise.record.utils.ResponseUtils.makeSuccessResponse;

// 展示页面信息
@Controller
public class PageController {

    @Autowired
    private UserService userService;

    @GetMapping("/")
    public String redirectToGreetings() {
        return "redirect:/greetings";
    }

    @GetMapping("/greetings")
    public String greetings() {
        return "greetings";  // 返回 greetings.html 视图
    }

    @GetMapping("/login")
    public String loginPage() {
        return "loginPage";
    }

    @PostMapping("/login")
    public String login(HttpServletRequest request, RedirectAttributes redirectAttributes, Model model) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        boolean rememberMe = request.getParameter("remember-me") != null;

        // 假设验证逻辑通过
        // 实际应用中，您需要添加实际的用户认证逻辑
        if (authenticate(username, password).getCode() == 200) {


            model.addAttribute("username", username);
            request.getSession().setAttribute("username", username);
//            return new ModelAndView("redirect:/record","success",model);
            return "redirect:/record";
        } else {
//            return new ModelAndView("loginPage", "error", "Invalid credentials");
            return "redirect:/login";
        }

    }

    private Result authenticate(String username, String password) {
        // 这里应包含实际的认证逻辑，例如与数据库中存储的哈希密码比较
        User user = userService.loginService(username, password);
        if (user != null) {
            return makeSuccessResponse("登陆成功", user.getUsername());
        } else {
            return makeErrorResponse("用户名不存在或密码错误.");
        }
    }

    @GetMapping("/record")
    public String recordForUser(Model model, HttpServletRequest request) {
        String user = request.getSession().getAttribute("username").toString();
        model.addAttribute("username", user);
        return "recordForUser";
    }


    @GetMapping("/index")
    public String index(@RequestParam(required = false) String username,
                        @RequestParam(required = false) String email,
                        Model model) {
        List<User> users;
        if (username != null && !username.isEmpty()) {
            users = new ArrayList<>();
            users.add(userService.getByUsername(username));
        } else if (email != null && !email.isEmpty()) {
            // 根据输入邮箱模糊检索
            users = userService.findUsersByEmailContaining(email);
        } else {
            users = userService.listUser();
        }

        model.addAttribute("users", users);
        // 临时主页
        return "index";
    }

//    @GetMapping("/")
//    public String index(){
//        return "index";
//    }

}
